package si.unimb.src.wifisms.ui;

import java.util.ArrayList;

import si.unimb.src.wifisms.entity.Account;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class AccountArrayAdapter extends ArrayAdapter<Account> {

	private ArrayList<Account> contentAll;
	private Context context;
	private int resourceId;
	private View v;
	
	public AccountArrayAdapter(Context context, int textViewResourceId, ArrayList<Account> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.contentAll = objects;
		this.resourceId = textViewResourceId;
	}
	
	/**
	 * Funkcija poveže podatke z View-i
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		v = convertView;

		/* nastavimo pravi layout */
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(this.resourceId, null);
		}

		Account acc = contentAll.get(position);
		if (acc != null) {
			
			/* dobimo TextView, ki ga želimo in v njega zapišemo podatke */
			TextView descr = (TextView)v.findViewById(android.R.id.text1);
			TextView username = (TextView)v.findViewById(android.R.id.text2);
			descr.setText(acc.getDescription());
			username.setText("<" + acc.getUsername() + ">");
		}
		
		return v;
	}
}
